package com.itown.expo.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import webframe.common.file.ExportDataHelper;
import webframe.common.file.RowMapperFactory;
import webframe.common.helper.Code2NameHelper;
import webframe.common.helper.Code2NameImportEntry;
import webframe.common.helper.ControllerHelper;
import webframe.common.helper.FormatTransformHelper;

import com.itown.expo.model.Decl;
import com.itown.expo.model.Goods;
import com.itown.expo.model.GoodsSituation;

public class StatDeclListExcelView extends AbstractExcelView {
	private static Log logger = LogFactory.getLog(StatDeclListExcelView.class);
	private static Map<String, Code2NameImportEntry> c2NStorge;

	private RowMapperFactory rowMapperFactory;

	public RowMapperFactory getRowMapperFactory() {
		return rowMapperFactory;
	}

	public void setRowMapperFactory(RowMapperFactory rowMapperFactory) {
		this.rowMapperFactory = rowMapperFactory;
	}

	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		WebApplicationContext wc = getWebApplicationContext();
		if (c2NStorge == null) {
			c2NStorge = Code2NameHelper.getCode2StringStorage(wc);
			// style
		}
		response.setContentType("APPLICATION/OCTET-STREAM");
		String filename = "报检单列表-" + FormatTransformHelper.currentDate()
				+ ".xls";
		filename = new String(filename.getBytes("GB18030"), "ISO8859-1");
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ filename + "\"");
		logger.info("导出>报检单列表-货物列表-疫情列表");
		// export
		ExportDataHelper.exportDataToExcel(c2NStorge, workbook, "报检单列表",
				(List) model.get("declList"), rowMapperFactory.getRowMapper(wc,
						Decl.class));
		ExportDataHelper.exportDataToExcel(c2NStorge, workbook, "货物列表",
				(List) model.get("goodsList"), rowMapperFactory.getRowMapper(
						wc, Goods.class));
		ExportDataHelper.exportDataToExcel(c2NStorge, workbook, "疫情列表",
				(List) model.get("goodsSituationList"), rowMapperFactory
						.getRowMapper(wc, GoodsSituation.class));
		customWorkSheet1(workbook, (List<Decl>) model.get("declList"));

	}

	// 上海局世博专用物资进境统计表
	public static void customWorkSheet1(HSSFWorkbook workbook,
			List<Decl> declList) {
		HSSFSheet sheet = workbook.createSheet("上海局世博专用物资进境统计表");
		HSSFCellStyle headerStyle = ExportDataHelper.getHeaderStyle(workbook);
		HSSFCellStyle rowStyle = ExportDataHelper.getRowStyle(workbook);
		int rowNum = -1;
		int colNum = -1;
		HSSFRow header = sheet.createRow(++rowNum); // 第1行
		ExportDataHelper.setStyleToRow(header, headerStyle);

		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("报检日期"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("报检号"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("品名"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("输出国/地区"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("用途"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("报检单位"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("重量"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("重量单位"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("数量"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("数量单位"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("货物总值(美元)"));
		header.createCell((short) ++colNum).setCellValue(
				new HSSFRichTextString("备注"));
		ExportDataHelper.setStyleToRow(header, headerStyle);

		HSSFRow row;
		for (Decl decl : declList) {
			colNum = -1;
			row = sheet.createRow(++rowNum);
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(FormatTransformHelper
							.date2String(decl.getDeclDate())));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(decl.getDeclNo()));
			row.createCell((short) ++colNum)
					.setCellValue(
							new HSSFRichTextString(decl.getFirstGoods()
									.getGoodsCname()));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(Code2NameHelper.getNameByCode(
							"country", decl.getTradeCountryCode())));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(Code2NameHelper.getNameByCode(
							"backupCode", decl.getBackupCode())));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(decl.getEntName()));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(ControllerHelper.safeString(decl
							.getFirstGoods().getWeight())));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(Code2NameHelper
							.getNameByCode("measure", decl.getFirstGoods()
									.getWeightUnitCode())));

			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(ControllerHelper.safeString(decl
							.getFirstGoods().getQty())));
			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(Code2NameHelper.getNameByCode(
							"measure", decl.getFirstGoods().getQtyUnitCode())));

			row.createCell((short) ++colNum).setCellValue(
					new HSSFRichTextString(decl.getValuesUsd()));
			ExportDataHelper.setStyleToRow(row, rowStyle);
		}

	}
}
